import * as THREE from "three";

export function CreateGeometryMeshWithColor(vertices:Float32Array,indices:Uint16Array,vertexColors:Float32Array):THREE.Mesh{
  
    // 创建 BufferGeometry
    const geometry = new THREE.BufferGeometry();
  
    // 设置顶点属性
    geometry.setAttribute('position', new THREE.BufferAttribute(vertices, 3));
  
    // 设置索引
    geometry.setIndex(new THREE.BufferAttribute(indices, 1));
  
    if(vertexColors){
        // 设置顶点颜色
        geometry.setAttribute('color', new THREE.BufferAttribute(vertexColors, 3));    
        // 创建材质，启用顶点颜色
        const material = new THREE.MeshBasicMaterial({ vertexColors: true });    
        // 创建网格
        const triangleMesh = new THREE.Mesh(geometry, material);
        
        return triangleMesh;
    }
    else{

        // 创建材质
        const material = new THREE.MeshBasicMaterial({ color: 0x00ff00, wireframe: false });
        // 创建网格
        const triangleMesh = new THREE.Mesh(geometry, material);

        return triangleMesh;
    }
    
}

export function CreateGeometryMesh(vertices:Float32Array,indices:Uint16Array):THREE.Mesh{
  
    // 创建 BufferGeometry
    const geometry = new THREE.BufferGeometry();
  
    // 设置顶点属性
    geometry.setAttribute('position', new THREE.BufferAttribute(vertices, 3));
  
    // 设置索引
    geometry.setIndex(new THREE.BufferAttribute(indices, 1));
  


    // 创建材质
    const material = new THREE.MeshBasicMaterial({ color: 0xffffff, wireframe: false });
    // 创建网格
    const triangleMesh = new THREE.Mesh(geometry, material);

    return triangleMesh;

    
}

export function CreateGeometry(vertices:Float32Array,indices:Uint16Array):THREE.BufferGeometry{
  
    // 创建 BufferGeometry
    const geometry = new THREE.BufferGeometry();
  
    // 设置顶点属性
    geometry.setAttribute('position', new THREE.BufferAttribute(vertices, 3));
  
    // 设置索引
    geometry.setIndex(new THREE.BufferAttribute(indices, 1));
  
    return geometry;  
}

export function CreateGeometryWithColor(vertices:Float32Array,indices:Uint16Array,vertexColors:Float32Array):THREE.BufferGeometry{
  
    // 创建 BufferGeometry
    const geometry = new THREE.BufferGeometry();
  
    // 设置顶点属性
    geometry.setAttribute('position', new THREE.BufferAttribute(vertices, 3));
  
    // 设置索引
    geometry.setIndex(new THREE.BufferAttribute(indices, 1));

    // 设置顶点颜色
    geometry.setAttribute('color', new THREE.BufferAttribute(vertexColors, 3));    
  
    return geometry;  
}

export function CreateGeometryWithColor4(vertices:Float32Array,indices:Uint16Array,vertexColors:Float32Array):THREE.BufferGeometry{
  
    // 创建 BufferGeometry
    const geometry = new THREE.BufferGeometry();
  
    // 设置顶点属性
    geometry.setAttribute('position', new THREE.BufferAttribute(vertices, 3));
  
    // 设置索引
    geometry.setIndex(new THREE.BufferAttribute(indices, 1));

    // 设置顶点颜色
    geometry.setAttribute('color', new THREE.BufferAttribute(vertexColors, 4));    
  
    return geometry;  
}


export function CreateGeometryWithUV(vertices:Float32Array,indices:Uint16Array,uvs:Float32Array):THREE.BufferGeometry{
  
    // 创建 BufferGeometry
    const geometry = new THREE.BufferGeometry();
  
    // 设置顶点属性
    geometry.setAttribute('position', new THREE.BufferAttribute(vertices, 3));
  
    // 设置索引
    geometry.setIndex(new THREE.BufferAttribute(indices, 1));

    // 设置UV
    geometry.setAttribute('uv', new THREE.BufferAttribute(uvs, 2));
  
    return geometry;  
}

function createTriangleMesh(){
    // 定义顶点和索引数据
    const vertices = new Float32Array([
        // 三角形的3个顶点
        -1, -1, 0,
        1, -1, 0,
        0,  1, 0
    ]);
  
    // 定义顶点颜色
    const colors = new Float32Array([
        1.0, 0.0, 0.0,  // 顶点 1：红色
        0.0, 1.0, 0.0,  // 顶点 2：绿色
        0.0, 0.0, 1.0   // 顶点 3：蓝色
    ]);
  
    const indices = new Uint16Array([
        // 三角形的三个索引
        0, 1, 2
    ]);
  
  
    // 创建 BufferGeometry
    const geometry = new THREE.BufferGeometry();
  
    // 设置顶点属性
    geometry.setAttribute('position', new THREE.BufferAttribute(vertices, 3));
  
    // 设置索引
    geometry.setIndex(new THREE.BufferAttribute(indices, 1));
  
    // 设置顶点颜色
    geometry.setAttribute('color', new THREE.BufferAttribute(colors, 3));
  
    
    // const material = new THREE.MeshBasicMaterial({ color: 0x00ff00, wireframe: false });
    // 创建材质，启用顶点颜色
    const material = new THREE.MeshBasicMaterial({ vertexColors: true });
  
    // 创建网格
    const triangleMesh = new THREE.Mesh(geometry, material);
    
    return triangleMesh;
}
  
  
export function createCubeMesh(){
    // 定义顶点和索引数据
    const vertices = new Float32Array([
        // 立方体的8个顶点
        -1, -1, -1,
        1, -1, -1,
        1,  1, -1,
        -1,  1, -1,
        -1, -1,  1,
        1, -1,  1,
        1,  1,  1,
        -1,  1,  1
    ]);
  
    const indices = new Uint16Array([
        // 立方体的12个三角形索引
        0, 1, 2, 0, 2, 3, // 后面
        4, 5, 6, 4, 6, 7, // 前面
        0, 1, 5, 0, 5, 4, // 底面
        2, 3, 7, 2, 7, 6, // 顶面
        0, 3, 7, 0, 7, 4, // 左面
        1, 2, 6, 1, 6, 5  // 右面
    ]);
  
    // 创建 BufferGeometry
    const geometry = new THREE.BufferGeometry();
  
    // 设置顶点属性
    geometry.setAttribute('position', new THREE.BufferAttribute(vertices, 3));
  
    // 设置索引
    geometry.setIndex(new THREE.BufferAttribute(indices, 1));
  
    // 创建材质
    const material = new THREE.MeshBasicMaterial({ color: 0x00ff00, wireframe: false });
  
    // 创建网格
    const cubeMesh = new THREE.Mesh(geometry, material);
    
    return cubeMesh;
}
  
function createPlaneFromBufferGeometry(){
    // 使用bufferGeometry创建面
    const bufferGeometry = new THREE.BufferGeometry();
    // 创建顶点数组，顶点存在顺序，一般为逆时针，每三个为一组
    const vertices = new Float32Array([
        -1.0, -1.0, 0.0, 
        1.0, -1.0, 0.0,
        1.0, 1.0, 0.0,
        -1.0,1.0,0.0,
    ]);
    // console.log(vertices.byteLength);
    // console.log(vertices.length);

    // 创建顶点属性
    bufferGeometry.setAttribute("position", new THREE.BufferAttribute(vertices, 3));

    // 创建索引
    const indices = new Uint16Array([0,1,2,2,3,0]);
    // 创建索引属性
    bufferGeometry.setIndex(new THREE.BufferAttribute(indices,1));

    // 创建材质
    const bgMaterial = new THREE.MeshBasicMaterial({
        color: 0x00ff00,
        wireframe: false,
        // side:THREE.DoubleSide,//两面都可以看到
    });
    const bgMaterial1 = new THREE.MeshBasicMaterial({
        color: 0xff0000,
    });


    // 设置两个组
    // BufferGeometry.addGroup(start,count,materialIndex);{ start: Integer, count: Integer, materialIndex: Integer }
    bufferGeometry.addGroup(0,3,0);
    bufferGeometry.addGroup(3,3,1);

    // console.log(bufferGeometry);

    const plane = new THREE.Mesh(bufferGeometry, [bgMaterial,bgMaterial1]);
    return plane;
}


export function CreateSphereMesh(x:number,y:number,z:number){
    // 创建球体
    const geometry = new THREE.SphereGeometry(100, 32, 32);
    const material = new THREE.MeshBasicMaterial({ color: 0x00ff00 });
    const sphere = new THREE.Mesh(geometry, material);
    sphere.position.set(x, y, z);
    return sphere;
}